package com.kb.medical.service.impl;

import com.kb.medical.entity.PdfImage;
import com.kb.medical.entity.UserFile;
import com.kb.medical.mapper.mp.PdfImageMapper;
import com.kb.medical.service.PdfImageService;
import com.kb.medical.service.ex.InsertException;
import com.kb.medical.util.ocrImg.OcrCode;
import com.kb.medical.util.ocrImg.OcrEntry;
import com.kb.medical.util.ocrImg.OcrResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.kb.medical.util.Text_Pdf.getNumberFromString;

@Service//@Service注解：将当前类的对象交给Spring来管理，自动创建对象
public class PdfImageServiceImpl implements PdfImageService {
    @Resource
    private PdfImageMapper pdfImageMapper;


    @Override
    public void pdfImageInsert(PdfImage pdfImage) {
        Integer rows = pdfImageMapper.pdfImageInsert(pdfImage);
        if (rows != 1) {
            throw new InsertException("在插入数据过程中产生了未知异常");
        }
    }


    @Override
    public PdfImage ImageFindByDocumentId(int documentId) {
        PdfImage result = pdfImageMapper.ImageFindByDocumentId(documentId);
        return result;
    }

    @Override
    public void TextFromImg(UserFile userFile, PdfImage pdfImage) {

        //记录开始识别时间
//        long startTime = System.currentTimeMillis();

        // paddleocr_json 的可执行文件所在路径
        System.out.println(System.getProperty("user.dir"));
        //传输相对路径
        String exePath = "D:\\javacode\\medical\\src\\main\\java\\com\\kb\\medical\\util\\ocrImg\\PaddleOCR-json.v1.2.1\\PaddleOCR_json.exe";

        // 可选的配置项
        Map<String, Object> arguments = new HashMap<>();
        // arguments.put("use_angle_cls", true);

        // 初始化 OCR
        try (com.kb.medical.util.ocrImg.Ocr ocr = new com.kb.medical.util.ocrImg.Ocr(new File(exePath), arguments)) {
            // 图像所在文件夹路径
            String imgFolder = pdfImage.getPath();
            String result = "";
            // 获取文件夹中所有png格式的图像
            File[] files = new File(imgFolder).listFiles((dir, name) -> name.endsWith(".png"));

            // 循环运行OCR并输出结果
            for (File file : files) {
                OcrResponse resp = ocr.runOcr(file);

                pdfImage.setPage(getNumberFromString(file.getName())); //获取该文件的所在页数

                if (resp.code == OcrCode.OK) {
                    for (OcrEntry entry : resp.data) {
                        pdfImage.setOcrText(entry.text);
                        pdfImage.setScore(userFile.getScore());
                        pdfImage.setFilename(userFile.getFileName());
                        pdfImage.setUploadUserName(userFile.getUploadUserName());
                        pdfImage.setClick(0);
                        pdfImage.setLike(0);
                        pdfImage.setUploadTime(new Date());
                        String localPath = userFile.getPdfPath();
                        pdfImage.setPath(localPath);
                        pdfImage.setFilename(userFile.getFileName());
                        System.out.println(pdfImage);
                        pdfImageInsert(pdfImage);
                    }
                } else {
                    System.out.println("error: code=" + resp.code + " msg=" + resp.msg);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //记录识别结束时间，并输出运行时长
//        long endTime = System.currentTimeMillis();
//        long totalTime = endTime - startTime;
//        String time = "总运行时长: " + totalTime + " ms";
//        return time;


    }
}
